package com.yanggu.jt808.entity.gpsdata;

import com.yanggu.jt808.util.Jt808Constant;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.Data;
import org.springframework.beans.BeanUtils;

/**
 * @author: 杨顾
 * @Description: 平台通用应答
 * @Version: 1.0
 */
@Data
public class Data0x8001 extends DataPackage {

    /**
     * 成功/确认
     */
    public static final byte SUCCESS = 0;

    /**
     * 失败
     */
    public static final byte FAILURE = 1;

    /**
     * 消息有误
     */
    public static final byte MSG_ERROR = 2;

    /**
     * 不支持
     */
    public static final byte UNSUPPORTED = 3;

    /**
     * 报警处理确认
     */
    public static final byte ALARM_PROCESS_ACK = 4;

    public static Data0x8001 response(DataPackage msg, int flowId, byte result) {
        if (result != SUCCESS && result != FAILURE && result != MSG_ERROR && result != UNSUPPORTED && result != ALARM_PROCESS_ACK) {
            throw new RuntimeException("传入的result有误");
        }
        Data0x8001 resp = new Data0x8001();
        Header header = msg.getHeader();
        Header respHeader = resp.getHeader();
        BeanUtils.copyProperties(header, respHeader);
        //设置消息ID
        respHeader.setMsgId(Jt808Constant.平台通用应答);
        //设置消息流水号
        respHeader.setFlowId(flowId);

        //封装返回的信息
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer(5);
        //应答流水号(2个字节)对应的终端消息的流水号
        byteBuf.writeShort(header.getFlowId());
        //应答ID(2个字节)对应的终端消息的ID
        byteBuf.writeShort(header.getMsgId());
        byteBuf.writeByte(result);
        //设置返回的消息
        resp.setMsgBody(byteBuf);
        return resp;
    }

}
